package com.qxzn.module.home.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lmc.util.DateFormatUtil;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.NewsMessageBean;
import com.qxzn.module.home.R;

import java.util.List;

public class NewsItemAdapter extends RecyclerView.Adapter<NewsItemAdapter.ViewHolder> {
    public static int NEWS_TYPE_DEFAULT = 0;
    public static int NEWS_TYPE_SEEK_MORE = 1;
    public static int NEWS_TYPE_CALLBACK = 2;
    private Activity activity;
    List<NewsMessageBean> dataList;

    public NewsItemAdapter(Activity activity, List<NewsMessageBean> robotDataList) {
        this.activity = activity;
        dataList = robotDataList;
    }

    @NonNull
    @Override
    public NewsItemAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(activity).inflate(
                viewType == NEWS_TYPE_SEEK_MORE ? R.layout.news_style_seek_more_layout : (
                        viewType == NEWS_TYPE_CALLBACK ? R.layout.news_style_callback_layout : R.layout.news_style_default_layout
                )
                , parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        NewsMessageBean newsMessageBean = dataList.get(position);
        Glide.with(activity).load(HttpRequest.BASE_URL + newsMessageBean.icon).apply(RequestOptions.circleCropTransform()
                .placeholder(R.drawable.icon_news).error(R.drawable.icon_news)).into(holder.icon);
//        holder.icon.setImageResource(R.drawable.logo);
        holder.time.setText(DateFormatUtil.getMessageDate(newsMessageBean.time));
        holder.content.setText(newsMessageBean.msg);
    }

    @Override
    public int getItemViewType(int position) {
        NewsMessageBean newsMessageBean = dataList.get(position);
        return newsMessageBean.type == 4 ? NEWS_TYPE_SEEK_MORE :
                (TextUtils.isEmpty(newsMessageBean.comment) ? NEWS_TYPE_DEFAULT : newsMessageBean.type);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView icon;
        TextView time;
        TextView content;

        public ViewHolder(View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.iv_news_icon);
            time = itemView.findViewById(R.id.tv_news_time);
            content = itemView.findViewById(R.id.tv_news_content);
        }
    }
}
